package com.lxq.blog.com.lxq.blog.entity;

import com.lxq.blog.com.lxq.blog.enums.RoleEnum;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

@Setter
@Getter
@ToString
@Entity
public class User implements UserDetails {
    private String nickName;
    @Id
    private String userName;
    private String password;
    private String avatar;
    private Date registerDate;
    @Enumerated(EnumType.STRING)
    @Column(columnDefinition = "enum('管理员','普通','VIP') default '普通'")
    private RoleEnum role;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<SimpleGrantedAuthority> roles= new ArrayList<>();
        roles.add(new SimpleGrantedAuthority(role.getRole()));
        return roles;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return userName;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
